//
//  STMainTabbarViewController.m
//  STBasicsApp
//
//  Created by jerry on 2019/5/6.
//  Copyright © 2019 jerry. All rights reserved.
//

#import "STMainTabbarViewController.h"
#import "STHomeTabViewController.h"
#import "STMeTabViewController.h"

@interface STMainTabbarViewController ()

@end

@implementation STMainTabbarViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化所有控制器
    [self setUpChildVC];
}

- (void)setUpChildVC {
    
    //设置主题颜色
    self.tabBar.dk_backgroundColorPicker = DKColorPickerWithKey(TABBARBG);
    self.tabBar.dk_barTintColorPicker    = DKColorPickerWithKey(TABBARBG);
    
    STHomeTabViewController *item01VC = [[STHomeTabViewController alloc] init];
    [self setChildVC:item01VC title:@"首页" image:@"st_icon_tabbar_item01_n" selectedImage:@"st_icon_tabbar_item01_s"];
    
    STMeTabViewController *item04VC = [[STMeTabViewController alloc] init];
    [self setChildVC:item04VC title:@"我的" image:@"st_icon_tabbar_item04_n" selectedImage:@"st_icon_tabbar_item04_s"];
    
}

- (void) setChildVC:(UIViewController *)childVC title:(NSString *) title image:(NSString *) image selectedImage:(NSString *) selectedImage {
    
    childVC.tabBarItem.title = title;
    NSMutableDictionary *dictN = [NSMutableDictionary dictionary];
    NSMutableDictionary *dictS = [NSMutableDictionary dictionary];
    
    dictN[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    dictS[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    
    if ([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNight]) {
        dictN[NSForegroundColorAttributeName] = UIColorFromRGB(THEME_KEY_NIGHT_COLOR_TABBARTEXTN);
        dictS[NSForegroundColorAttributeName] = UIColorFromRGB(THEME_KEY_NIGHT_COLOR_TABBARTEXTS);
    }else if ([self.dk_manager.themeVersion isEqualToString:THEME_KEY_RED]) {
        dictN[NSForegroundColorAttributeName] = UIColorFromRGB(THEME_KEY_RED_COLOR_TABBARTEXTN);
        dictS[NSForegroundColorAttributeName] = UIColorFromRGB(THEME_KEY_RED_COLOR_TABBARTEXTS);
    }else if ([self.dk_manager.themeVersion isEqualToString:THEME_KEY_GREEN]) {
        dictN[NSForegroundColorAttributeName] = UIColorFromRGB(THEME_KEY_GREEN_COLOR_TABBARTEXTN);
        dictS[NSForegroundColorAttributeName] = UIColorFromRGB(THEME_KEY_GREEN_COLOR_TABBARTEXTS);
    }else if ([self.dk_manager.themeVersion isEqualToString:THEME_KEY_YELLOW]) {
        dictN[NSForegroundColorAttributeName] = UIColorFromRGB(THEME_KEY_YELLOW_COLOR_TABBARTEXTN);
        dictS[NSForegroundColorAttributeName] = UIColorFromRGB(THEME_KEY_YELLOW_COLOR_TABBARTEXTS);
    }else{
        dictN[NSForegroundColorAttributeName] = UIColorFromRGB(THEME_KEY_NORMAL_COLOR_TABBARTEXTN);
        dictS[NSForegroundColorAttributeName] = UIColorFromRGB(THEME_KEY_NORMAL_COLOR_TABBARTEXTS);
    }
    
    [childVC.tabBarItem setTitleTextAttributes:dictN forState:UIControlStateNormal];
    [childVC.tabBarItem setTitleTextAttributes:dictS forState:UIControlStateHighlighted];
    
    childVC.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    STBaseNavigationViewController *nav = [[STBaseNavigationViewController alloc] initWithRootViewController:childVC];
    [self addChildViewController:nav];
    
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    
    NSLog(@"item name = %@", item.title);
    
    //NSInteger index = [self.tabBar.items indexOfObject:item];
    
    if([item.title isEqualToString:@"我的"])
    {
        // 也可以判断标题,然后做自己想做的事
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
